<?php

/**
 * This is the model class for table "{{checkout_form}}".
 *
 * The followings are the available columns in table '{{checkout_form}}':
 * @property integer $form_id
 * @property string $form_code
 * @property string $form_name
 * @property string $form_description
 * @property integer $form_status
 * @property integer $form_weight
 */
class CheckoutForm extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return CheckoutForm the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{checkout_form}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('form_code, form_name, form_description, form_weight', 'required'),
            array('form_status, form_weight', 'numerical', 'integerOnly' => true),
            array('form_code', 'length', 'max' => 50),
            array('form_name', 'length', 'max' => 100),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('form_id, form_code, form_name, form_description, form_status, form_weight', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'form_id' => 'Form',
            'form_code' => 'Form Code',
            'form_name' => 'Form Name',
            'form_description' => 'Form Description',
            'form_status' => 'Form Status',
            'form_weight' => 'Form Weight',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('form_id', $this->form_id);
        $criteria->compare('form_code', $this->form_code, true);
        $criteria->compare('form_name', $this->form_name, true);
        $criteria->compare('form_description', $this->form_description, true);
        $criteria->compare('form_status', $this->form_status);
        $criteria->compare('form_weight', $this->form_weight);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public static function GetListForm() {
        $criteria = new CDbCriteria;
        $criteria->order = 't.form_weight ASC';
        return CheckoutForm::model()->findAll($criteria);
    }

    public static function ajaxChangeOrderModule() {
        if (Yii::app()->request->isPostRequest) {
            if (isset($_POST['data']) && (strpos($_POST['data'], "li_item_id[]") !== false)) {

                $string_explode_order = explode("&", $_POST['data']);
                $order = array();

                foreach ($string_explode_order as $order_item) {
                    $order_explode = explode("=", $order_item);
                    if (isset($order_explode[1]) && $order_explode[1] != 0)
                        $order[] = $order_explode[1];
                }

                $command = Yii::app()->db->createCommand();
                foreach ($order as $order => $id) {

                    $command->update('gxc_checkout_form', array(
                        'form_weight' => $order + 1,
                            ), 'form_id=:id', array(':id' => $id));
                }
                echo json_encode(array('result' => t('cms', 'success'), 'message' => ''));
            } else {
                echo json_encode(array('result' => t('cms', 'error'), 'message' => t('cms', 'Error! Invalid Input Data')));
            }
        } else {
            echo json_encode(array('result' => t('cms', 'error'), 'message' => t('cms', 'Error! Invalid Request!')));
        }
        Yii::app()->end();
    }

}
